package cz.proks.easyhelpdesk.gwt.activity.base;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.place.shared.PlaceHistoryMapper;

import cz.proks.easyhelpdesk.api.model.User;
import cz.proks.easyhelpdesk.gwt.ClientFactory;
import cz.proks.easyhelpdesk.gwt.Globals;
import cz.proks.easyhelpdesk.gwt.place.base.HelpdeskPlace;
import cz.proks.easyhelpdesk.gwt.utils.CookieUtils;

public abstract class BaseActivity<ThePlace extends HelpdeskPlace> extends AbstractActivity {
        
    private final ThePlace place;
    private final ClientFactory clientFactory;
    private final PlaceHistoryMapper placeMapper;
    private long cookieMemberId = -1l;
    
    public BaseActivity(ThePlace place, ClientFactory clientFactory, PlaceHistoryMapper placeMapper) {
        super();
        this.place = place;
        this.clientFactory = clientFactory;
        this.placeMapper = placeMapper;
        initialize();
    }
    
    protected ThePlace getPlace() {
        return place;
    }
    
    protected ClientFactory getClientFactory() {
        return clientFactory;
    }     
    
    protected PlaceHistoryMapper getPlaceMapper() {
        return placeMapper;
    }
    
    private void initialize() {
        User user = Globals.getCurrentUser();
        Long memberCookieId = CookieUtils.getCookieUserId();        
        setCookieMemberId(user != null ? user.getId() : memberCookieId);
    }
    
    protected long getCookieMemberId() {
        return cookieMemberId;
    }
    
    public void setCookieMemberId(Long memberId) {
        this.cookieMemberId = memberId == null ? -1l : memberId;
        CookieUtils.setCookieUserId(cookieMemberId);
    }
    
    public void setCookieMemberId(User user) {
        setCookieMemberId(user != null ? user.getId() : null);
    }   
}
